// 需转化数组示例
let data = [
  { id: "01", lable: "项目经理", pid: "" },

  { id: "02", lable: "产品leader", pid: "01" },

  { id: "03", lable: "UIleader", pid: "01" },

  { id: "04", lable: "技术leader", pid: "01" },

  { id: "05", lable: "测试leader", pid: "01" },

  { id: "06", lable: "运维leader", pid: "01" },

  { id: "07", lable: "产品经理", pid: "02" },

  { id: "08", lable: "产品经理", pid: "02" },

  { id: "09", lable: "UI设计师", pid: "03" },

  { id: "10", lable: "前端工程师", pid: "04" },

  { id: "11", lable: "后端工程师", pid: "04" },

  { id: "12", lable: "后端工程师", pid: "04" },

  { id: "13", lable: "测试工程师", pid: "05" },

  { id: "14", lable: "测试工程师", pid: "05" },

  { id: "15", lable: "运维工程师", pid: "06" },
];

const array2tree = (arr) => {
  let tree = [],
    resObj = {};
  arr.forEach((item) => {
    resObj[item.id] = item;
  });

  arr.forEach((item) => {
    const parent = resObj[item.pid];
    if (parent) {
      (parent.child || (parent.child = [])).push(item);
    } else {
      tree.push(item);
    }
  });
  return tree;
};

console.log(JSON.stringify(array2tree(data)));



const array2tree2 = (arr) => {
  if (!Array.isArray(arr) || !arr.length) {
    return false;
  }

  let resObj = {},
    res = [];

  arr.forEach((item) => {
    resObj[item.id] = item;
  });

  arr.forEach((item) => {
    const parent = resObj[item.pid];
    if (parent) {
      (parent.child || (parent.child = [])).push(item);
    } else {
      res.push(item);
    }
  });
  return res;
};





function array2Tree(arr){
  if(!Array.isArray(arr)){
    return {}
  }

  let map = {},res=[]
  arr.forEach(item=>{
    map[item.id]=item
  })


  arr.forEach(item =>{
    const parent  = map[item.pid]
    if(parent){
      (item.child || (item.child=[])).push(item)
    }else {
      res.push(item)
    }
  })
  return res;
}



function array2Tree(arr){
  if(!Array.isArray(arr)) return new TypeError('typeError')

  let map ={}, res=[]
  arr.forEach(item=>{
    map[item.id]=item
  })

  arr.forEach(item=>{
    const parent = map[item.pid]
    if(parent){
      (parent.child||(parent.child=[])).push(item)
    }else {
      res.push(item)
    }
  })
  return res;
}


function array2tree(arr){
  if(!Array.isArray(arr)) return TypeError('arg must a array')


  let res = [], map = {}
  arr.forEach(item => {
    map[item.id] = item
  })

  arr.forEach(item => {
    const parent = map[item.parentId];
    if(parent){
      (parent.child||(parent.child=[])).push(item)
    }else {
      res.push(item)
    }
  })
  return  res;
}



function array2Tree(arr) {
  let res = [],
    map = {};

  arr.forEach((item) => {
    map[item.id] = item;
  });

  arr.forEach((item) => {
    const parent = map[item.parentId];
    if (parent) {
      (parent.child || (parent.child = [])).push(item);
    } else {
      res.push(item);
    }
  });
  return res;
}


const array2Tree3 = (arr) => {
  const res = [],
    map = {};

  arr.forEach((item) => {
    map[item.id] = item;
  });

  arr.forEach((item) => {
    const parent = map[item.pid];
    if (parent) {
      (parent.child || (parent.child = [])).push(item);
    } else {
      res.push(item);
    }
  });
  return res;
};